package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceFrrService;
import com.eastjet.equipment.service.DeviceMoService;
import com.eastjet.maintenance.domain.DeviceFrr;
import com.eastjet.maintenance.domain.DeviceMo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/mo")
public class MoController {
    @Autowired
    private DeviceMoService deviceMoService ;

    @RequestMapping("/list")
    public Page<DeviceMo> getDeviceFrrList(@RequestParam(defaultValue = "1") int page,
                                           @RequestParam(defaultValue = "2") int size,
                                           @RequestParam(defaultValue ="") Map<String,Object> map)
    {
        System.out.println("map:" + map);
//        Page<DeviceFrr> pageData = deviceFrrService.getDeviceFrrList(page, size, condition);
        return deviceMoService.getDeviceMoList(page, size, map);
    }
    @RequestMapping("/add")
    public String addMo( @RequestParam(defaultValue ="")  Map<String, Object> map) {

        String TableName;
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.get("TableName") == null || map.isEmpty()){
            //返回错误信息
            return "TableName is null or empty";
        }else
        {
            TableName = (String) map.get("TableName");
           DeviceMo deviceMo = new DeviceMo();
            deviceMo.setMoState((String) map.get("state"));
            deviceMo.setCode((String) map.get("code"));
            // 使用 Integer.valueOf 转换整数
            deviceMo.setMoTopic((String) map.get("topic"));
            deviceMo.setMoType((String) map.get("type"));
            deviceMo.setMoFrrId((String) map.get("frrId"));
            deviceMo.setMoClass(String.valueOf(Integer.valueOf( map.get("class").toString())));
            deviceMo.setMoPerson((String) map.get("person"));
            deviceMo.setMoOther((String) map.get("other"));
            deviceMo.setNoet((String) map.get("noet"));
            deviceMo.setEquipmentId(Integer.valueOf( map.get("equipmentId").toString()));
            deviceMo.setDescription((String) map.get("description"));
            deviceMo.setMoMoney((BigDecimal) map.get("money"));
            System.out.println("deviceMo:" + deviceMo);
            return   deviceMoService.addDeviceMo(deviceMo,TableName);

        }

    }


    @RequestMapping("/delete")
    public String deleteFT( @RequestParam(defaultValue ="")  Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceMo deviceMo = new DeviceMo();
        deviceMo.setMoId(Integer.valueOf(map.get("id").toString()));
        deviceMo.setIsDelete(Integer.valueOf(map.get("isDelete").toString()));
        return  deviceMoService.deleteDeviceMo(deviceMo);
    }
}
